﻿using Dynamics.NAV.Metro.Common;
using Dynamics.NAV.Metro.Data;
using Okra.Core;
using Okra.Navigation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Contacts.Provider;

namespace Dynamics.NAV.Metro.Pages.ContactPicker
{
    public class ContactPickerViewModel : BindableBase
    {
        private IList<NavCustomer> _customers;
        internal ContactPickerUI _contactPickerUI = null;
        
        public ContactPickerViewModel()
        {
            this.GoBackCommand = new DelegateCommand(GoBack);
            
            NavCustomerDataSource navDataSource = new NavCustomerDataSource();
            this.Customers = navDataSource.GetCustomers();
        }

        public ContactPickerViewModel(ContactPickerActivatedEventArgs args)
        {
            _contactPickerUI = args.ContactPickerUI;

            NavCustomerDataSource navDataSource = new NavCustomerDataSource();
            this.Customers = navDataSource.GetCustomers();
        }

        [Import]
        public IDynamicsNavApi NavApi { get; set; }

        public string DesiredField
        {
            get
            {
                if (_contactPickerUI != null)
                {
                    if (_contactPickerUI.DesiredFields.Count > 0)
                    {
                        return _contactPickerUI.DesiredFields[0];
                    }
                }

                return "Email";
            }
        }

        [Import]
        public INavigationManager NavigationManager { get; set; }
 
        public ICommand GoBackCommand { get; private set; }

        public IList<NavCompany> Companies
        {
            get 
            {
                if (NavApi == null)
                    NavApi = new DynamicsNavApi();
                return NavApi.Companies; 
            }
        }

        public NavCompany CurrentCompany
        {
            get 
            {
                if (NavApi == null)
                    NavApi = new DynamicsNavApi();

                return NavApi.CurrentCompany; 
            }
            set
            {
                if (NavApi == null)
                    NavApi = new DynamicsNavApi();
                
                NavApi.CurrentCompany = value;
                SettingsStorage.SetLocalSetting(App.DEFAULT_COMPANY_KEY, value.Name);
                AsyncHelpers.RunSync(() => LocalStorage.Save());
            }
        }

        public bool CanGoBack
        {
            get { return NavigationManager.CanGoBack; }
        }

        public void GoBack()
        {
            NavigationManager.GoBack();
        }

        [ImportingConstructor]
        public ContactPickerViewModel(NavCustomerDataSource navDataSource)
        {
            this.Customers = navDataSource.GetCustomers();
        }

        public ContactPickerUI ContactPicker
        {
            get { return _contactPickerUI; }
        }

        public IList<NavCustomer> Customers
        {
            get
            {
                return _customers;
            }
            set
            {
                if (_customers != value)
                {
                    _customers = value;
                    OnPropertyChanged("Customers");
                }
            }
        }
    }
}
